﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.CMS.Common.Utility;
using Runda.CMS.Common.Entity;
using Runda.CMS.Bll;
using System.Text;

namespace Runda.CMS.Portal.Workspace
{
    public partial class Notice_Edit : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) {
                if (Request.QueryString["NoticeID"] != null && Request.QueryString["NoticeID"].ToString() != "") {
                    NoticeID.Text = Request.QueryString["NoticeID"].ToString();
                    InitForm(NoticeID.Text);
                }
            }
        }

        private void InitForm(String strNoticeID)
        {
            SystemBll bll = new SystemBll();
            CtrtMyNoticeEntity noticeEntity = bll.Get<CtrtMyNoticeEntity>(new Guid(strNoticeID));
            NoticeTime.Text = noticeEntity.NoticeTime.ToString();
            //NoticeContent.InnerHtml = noticeEntity.NoticeContent;
            txtNoticeContent.Text = noticeEntity.NoticeContent;

            List<CtrtMyNoticeDepartmentEntity> list = bll.GetAll<CtrtMyNoticeDepartmentEntity>(" and NoticeID='" + strNoticeID + "'");
            if (list != null)
            {
                StringBuilder sbName = new StringBuilder();
                StringBuilder sbValue = new StringBuilder();
                foreach (CtrtMyNoticeDepartmentEntity en in list)
                {
                    if (sbName.Length != 0) sbName.Append(";");
                    if (sbValue.Length != 0) sbValue.Append(";");

                    sbName.Append(en.DepartmentName);
                    sbValue.Append(en.DepartmentID);
                }

                txtDeptID.Value = sbValue.ToString();
                txtDeptName.Text = sbName.ToString();
            }
        }

        protected void lnkbtnSave_Click(object sender, EventArgs e)
        {
            if (checkdata() == 0) { return; }
            Save();
            ClientScript.RegisterStartupScript(this.GetType(), "saveandclose", "alert('操作成功');window.opener.document.getElementById('ctl00_cphBody_btnRefersh').click();", true);
        }

        protected void lnkbtnSaveAndClose_Click(object sender, EventArgs e)
        {
            if (checkdata() == 0) { return; }
            Save();

            ClientScript.RegisterStartupScript(this.GetType(), "saveandclose", "window.opener.document.getElementById('ctl00_cphBody_btnRefersh').click();window.close();", true);
        }

        protected void lnkbtnSaveAndNew_Click(object sender, EventArgs e)
        {
            if (checkdata() == 0) { return; }
            Save();

            ClientScript.RegisterStartupScript(this.GetType(), "saveandclose", "alert('操作成功'); window.document.URL = '../workspace/Notice_edit.aspx';", true);
            
        }

        private int checkdata()
        {
            int rnt = 1;
            if (this.txtNoticeContent.Text.Trim() == "")
            {
                rnt = 0;
                ClientScript.RegisterStartupScript(this.GetType(), "saveandclose", "alert('请输入公告内容');", true);
                return rnt;
            }

            if (this.txtDeptName.Text.Trim() == "")
            {
                rnt = 0;
                ClientScript.RegisterStartupScript(this.GetType(), "saveandclose", "alert('请指定公告接受部门');", true);
                return rnt;
            }
            return rnt;

        }
        private void Save()
        {
            SystemBll noticeBll = new SystemBll();
            CtrtMyNoticeEntity noticeEntity;

            if (NoticeID.Text != "")
            {
                noticeEntity = noticeBll.Get<CtrtMyNoticeEntity>(new Guid(NoticeID.Text));
            }
            else {
                noticeEntity = new CtrtMyNoticeEntity();
                noticeEntity.NoticeID = Guid.NewGuid();
                noticeEntity.CreatedBy = base.CurrentUserID;
                noticeEntity.CreatedOn = System.DateTime.Now;      
                noticeEntity.Deleted = false;      
                noticeEntity.NoticeType = System.Convert.ToInt32(Enumerator.NoticeMessageType.Public);
            }       
            if (NoticeTime.Text != "")
            {
                noticeEntity.NoticeTime = System.Convert.ToDateTime(NoticeTime.Text);
            }
            else
            {
                noticeEntity.NoticeTime = System.DateTime.Now;
            }
            noticeEntity.NoticeContent = txtNoticeContent.Text;

            List<CtrtMyNoticeDepartmentEntity> list = new List<CtrtMyNoticeDepartmentEntity>();
            CtrtMyNoticeDepartmentEntity dept = null;
            if (txtDeptID.Value != null && txtDeptName.Text != null)
            {
                string[] names = txtDeptName.Text.Split(new char[] { ';' });
                string[] values = txtDeptID.Value.Split(new char[] { ';' });

                if (names.Length == values.Length)
                {
                    for (int i=0;i<values.Length ;i++)
                    {
                        if (string.IsNullOrEmpty(values[i].Trim())) continue;

                        dept = new CtrtMyNoticeDepartmentEntity();
                        dept.NoticeID = noticeEntity.NoticeID;
                        dept.DepartmentID = new Guid(values[i].Trim());
                        dept.DepartmentName = names[i];
                        dept.Deleted = false;

                        list.Add(dept);
                    }
                }
            }
            noticeEntity.CtrtMyNoticeDepartmentEntityList = list;

            noticeBll.SaveNotice(noticeEntity);


        }
    }
}
